/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package warehouse;

import ejb.warehouse.*;
import java.util.List;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionErrors;

import system.Utilities;

import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import system.LoginAction;

/**
 *
 * @author ciummo
 */
public class OrderProductAction extends DispatchAction {

    /*
     * forward name="success" path=""
     */
    private final static String SUCCESS = "success";
    
    private final static String TONEWORDERPRODUCT = "toNewOrderProduct";
    private final static String SUCCESSINSERTORDERPRODUCT = "successInsertOrderProduct";
    private final static String TOPRODUCTORDERDETAILS = "toProductOrderDetails";    
    private final static String TOMODIFYPRODUCTORDER ="toModifyProductOrder";
    private final static String USERDLISTORDERPRODUCT = "userDListOrderProduct";
    
    private final static String TOPRODUCTORDERDETAILSUSER = "userDialogListOrderProduct";
    
    
    private final static String USERDIALOG = "Tecnico Dialog";
    
    
    
    private Utilities utility = new Utilities();
    
    /**
     * This is the Struts action method called on
     * http://.../actionPath?method=myAction1, where "method" is the value
     * specified in <action> element : ( <action parameter="method" .../> )
     */
    
        public ActionForward listOrderProduct(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
            
            OrderProductActionForm orderProductForm = (OrderProductActionForm) form;
            
            
            OrdineProdottoFacadeRemote ordineProdottoFacade = (OrdineProdottoFacadeRemote) utility.callRemoteBean("OrdineProdottoFacade/remote");
                                   
            
            // TODO : Inserire il controllo del ruolo sessione, gli addetti possono vedere solo determinate cose!
            
            //orderProductForm.setList(ordineProdottoFacade.findOrdineProdottiByState("Incompleto"));         
        
            orderProductForm.setList(ordineProdottoFacade.findAll());
        
        return mapping.findForward(USERDLISTORDERPRODUCT);
    }   
    
    
    public ActionForward newOrderProduct(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        int idOrder = Integer.parseInt(request.getParameter("idOrder"));
        
        OrderProductActionForm orderProductForm = (OrderProductActionForm) form;
        
        orderProductForm.setOrdineidOrdine(idOrder);
        
        request.getSession().setAttribute("OrderProductActionForm", orderProductForm);      
        
        ArticoloFacadeRemote articoloFacade = (ArticoloFacadeRemote) utility.callRemoteBean("ArticoloFacade/remote");

        ArticleActionForm articoli = new ArticleActionForm();
        
        articoli.setList(articoloFacade.findAll());
        
        
        
        request.getSession().setAttribute("ListaArticoli", articoli);  
        
        return mapping.findForward(TONEWORDERPRODUCT);
    }

    /**
     * This is the Struts action method called on
     * http://.../actionPath?method=myAction2, where "method" is the value
     * specified in <action> element : ( <action parameter="method" .../> )
     */
    public ActionForward submitOrderProduct(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        OrderProductActionForm orderProductForm = (OrderProductActionForm) form;
        
        ActionErrors errors = form.validate(mapping, request);

        if (!errors.isEmpty()) {
            saveErrors(request, errors);
            return  mapping.findForward(TONEWORDERPRODUCT);
        }
        
        
        OrdineProdottoFacadeRemote ordineProdottoFacade = (OrdineProdottoFacadeRemote) utility.callRemoteBean("OrdineProdottoFacade/remote");
        
        OrdineProdotto newOrderProduct = new OrdineProdotto();
        
        newOrderProduct.setCodArticolo(orderProductForm.getCodArticolo());
        newOrderProduct.setPrezzo(orderProductForm.getPrezzo());
        newOrderProduct.setQuantita(orderProductForm.getQuantita());
        newOrderProduct.setSconto(orderProductForm.getSconto());
        newOrderProduct.setUnitaMisura(orderProductForm.getUnitaMisura());
        newOrderProduct.setNote(orderProductForm.getNote());
        newOrderProduct.setOrdineidOrdine(orderProductForm.getOrdineidOrdine());
        newOrderProduct.setStato("In Creazione");
        
        
        ordineProdottoFacade.create(newOrderProduct);
         
        request.getSession().setAttribute("id", orderProductForm.getOrdineidOrdine());
        
        
        return mapping.findForward(SUCCESSINSERTORDERPRODUCT);
    }
    
        public ActionForward orderProductDetails(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
            int idOrderProduct;
            
            try {        
            idOrderProduct = Integer.parseInt(request.getParameter("idOrderProduct"));
            }catch (NumberFormatException nfe){
                
                idOrderProduct = Integer.parseInt(request.getSession().getAttribute("idOrderProduct").toString());
            }


            RetrieveOrderProductFormById retrieveOrderProduct = new RetrieveOrderProductFormById();

            OrderProductActionForm orderProductForm = retrieveOrderProduct.retrieve(idOrderProduct);



            ProdottoFacadeRemote prodottoFacade = (ProdottoFacadeRemote) utility.callRemoteBean("ProdottoFacade/remote");

            ProductActionForm prodottoForm = new ProductActionForm();

            prodottoForm.setList(prodottoFacade.findProdottiByIdOrdineProdotto(idOrderProduct));

            
            

            try{
            orderProductForm.setMancanti(orderProductForm.getQuantita()/prodottoForm.getList().size()*10);
            }catch (Exception e){
                
                orderProductForm.setMancanti(orderProductForm.getQuantita() - prodottoFacade.findProdottiByIdOrdineProdotto(idOrderProduct).size());
            }
            
            
            
            request.getSession().setAttribute("OrderProductActionForm", orderProductForm);

            request.getSession().setAttribute("ProductActionForm", prodottoForm);

            if (utility.checkRole(request).equals(USERDIALOG)){
                
                return mapping.findForward(TOPRODUCTORDERDETAILSUSER);
            }
            
            
            return mapping.findForward(TOPRODUCTORDERDETAILS);
    }
        
        
        
        
        
        // questa action è uguale a quella per visualizzare... POTREBBE aver senso tenerne solo una...
        // anche se poi con il sistema delle limitazioni non è più conveniente
        public ActionForward modifyOrderProduct(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        int idOrderProduct = Integer.parseInt(request.getParameter("idOrderProduct"));
        
        RetrieveOrderProductFormById retrieveOrderProduct = new RetrieveOrderProductFormById();
        
        OrderProductActionForm orderProductForm = retrieveOrderProduct.retrieve(idOrderProduct);
        
        request.getSession().setAttribute("OrderProductActionForm", orderProductForm);
        
        return mapping.findForward(TOMODIFYPRODUCTORDER);
    }
        
        public ActionForward submitModifyOrderProduct(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
                    
            OrderProductActionForm orderProductForm = (OrderProductActionForm) form;
        
            ModifyOrdineProdotto mop = new ModifyOrdineProdotto();
            
            mop.Modify(orderProductForm);
            
            
        
        return mapping.findForward(TOPRODUCTORDERDETAILS);
    }            
        
}




class RetrieveOrderProductFormById {
    
    
    Utilities utility = new Utilities();
    
    public OrderProductActionForm retrieve (int id) throws NamingException{
        
        
        OrderProductActionForm orderProductForm = new OrderProductActionForm();
        
        OrdineProdottoFacadeRemote ordineFacade = (OrdineProdottoFacadeRemote) utility.callRemoteBean("OrdineProdottoFacade/remote");

        
        //QUI DEVO METTERE UN TRY
        
        OrdineProdotto ordineProdotto = ordineFacade.find(id);
        
        //IF NULL...
        
        orderProductForm.setIdOrdineProdotto(ordineProdotto.getIdOrdineProdotto());
        orderProductForm.setCodArticolo(ordineProdotto.getCodArticolo());
        orderProductForm.setPrezzo(ordineProdotto.getPrezzo());
        orderProductForm.setQuantita(ordineProdotto.getQuantita());
        orderProductForm.setSconto(ordineProdotto.getSconto());
        orderProductForm.setUnitaMisura(ordineProdotto.getUnitaMisura());
        orderProductForm.setNote(ordineProdotto.getNote());
        orderProductForm.setOrdineidOrdine(ordineProdotto.getOrdineidOrdine());
        orderProductForm.setStato(ordineProdotto.getStato());    
        
        return orderProductForm;
    }
 }   
    
class ModifyOrdineProdotto {
    
    
        Utilities utility = new Utilities();

    
    public boolean Modify (OrderProductActionForm orderProductForm) throws NamingException{
        
        
            OrdineProdottoFacadeRemote ordineProdottoFacade = (OrdineProdottoFacadeRemote) utility.callRemoteBean("OrdineProdottoFacade/remote");
        
            OrdineProdotto ordineProdotto = ordineProdottoFacade.find(orderProductForm.getIdOrdineProdotto());
            
            ordineProdotto.setPrezzo(orderProductForm.getPrezzo());
            ordineProdotto.setQuantita(orderProductForm.getQuantita());
            ordineProdotto.setSconto(orderProductForm.getSconto());
            ordineProdotto.setUnitaMisura(orderProductForm.getUnitaMisura());
            ordineProdotto.setNote(orderProductForm.getNote());
            
            ordineProdottoFacade.edit(ordineProdotto);
            
            return true;
            
        
    }
    
    
}    
    
    
